/**
 * 详情对话框
 */
var VocReducingPlanInfoDlg = {
    data: {
        planDesc: "",
        planType: ""
    }
};

layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/vocReducingPlan/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('vocReducingPlanForm', result.data);
    var param=$("input[name='planType']").val(),checkBoxs = $("input[type='checkbox']"), array = param.split(",");
    for (var i = 0; i < array.length; i++) {
        for (var j = 0; j < checkBoxs.length; j++) {
            var checkbox = $(checkBoxs[j]);
            if (checkbox.val() == array[i]) {
                checkbox.attr('checked','checked');
                break;
            }
        }
    }
    form.render('checkbox');

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/vocReducingPlan/editItem", function (data) {
            if(data.code == 200) {
                Feng.success("更新成功！");
            }else{
                Feng.error("更新失败！" + data.message)
            }

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("更新失败！" + data.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });





    form.on('checkbox(planType)', function (data) {
        var planType = $('input[name="planType"]'), value = data.value, array = planType.val().split(",");
        if (data.elem.checked) {
            planType.val(planType.val() + value + ",");
        } else {
            var newnumber = "";
            for (var i = 0; i < array.length; i++) {
                var str = array[i];
                newnumber += (str != value && str != "" && str != null) ? str + "," : "";
            }
            planType.val(newnumber);
        }
    });

});